#ifndef APPLICATIONINFO_H
#define APPLICATIONINFO_H

#include "appchat/chatmanager.h"
#include "model/friendlistmodel.h"
#include "model/conversationmodel.h"

#include <QtCore/QObject>
#include <QtQml/QQmlPropertyMap>
#include <QSortFilterProxyModel>

class ApplicationInfo : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString dpi READ dpi NOTIFY dpiChanged)
    Q_PROPERTY(qreal ratio READ ratio NOTIFY ratioChanged)

    Q_PROPERTY(QObject *colors READ colors CONSTANT)
    Q_PROPERTY(QObject *constants READ constants CONSTANT)

    Q_PROPERTY(bool isPortraitMode READ isPortraitMode WRITE setIsPortraitMode NOTIFY portraitModeChanged)
    Q_PROPERTY(int applicationWidth READ applicationWidth WRITE setApplicationWidth NOTIFY applicationWidthChanged)

    Q_PROPERTY(qint64 activeUid READ activeUid NOTIFY activeUidChanged)
    Q_PROPERTY(bool isLogin READ isLogin WRITE setIsLogin NOTIFY isLoginChanged)

    Q_PROPERTY(QString fullName READ fullName NOTIFY fullNameChanged)
    Q_PROPERTY(QString avatarUser READ avatarUser NOTIFY avatarUserChanged)

    Q_PROPERTY(FriendListModel* friendListModel READ friendListModel NOTIFY friendListModelChanged)
    Q_PROPERTY(FriendListModel* friendSearchModel READ friendSearchModel NOTIFY friendSearchModelChanged)
    Q_PROPERTY(FriendListModel* friendListSortByNameModel READ friendListSortByNameModel NOTIFY friendListSortByNameModelChanged)
    Q_PROPERTY(FriendListModel* friendListOnlineModel READ friendListOnlineModel NOTIFY friendListOnlineModelChanged)

public:
    explicit ApplicationInfo(QObject *parent = 0);

    QQmlPropertyMap *colors() const { return m_colors; }
    QQmlPropertyMap *constants() const { return m_constants; }

    bool isPortraitMode() const { return m_isPortraitMode; }
    void setIsPortraitMode(const bool newMode);
    qreal ratio() const { return m_ratio; }
    QString dpi() const { return m_dpi; }
    bool isLogin() const {return m_isLogin;}
    void setIsLogin(const bool isLogin);

    quint64 activeUid() const { return m_activeUid; }

    int applicationWidth() const { return m_applicationWidth; }
    void setApplicationWidth(const int newWidth);

    void initChatManager();


    FriendListModel* friendListModel() const
    {
        return m_friendListModel;
    }

    FriendListModel* friendSearchModel() const
    {
        return m_friendSearchModel;
    }

    FriendListModel* friendListSortByNameModel() const
    {
        return  m_friendListSortByNameModel;
    }

    FriendListModel* friendListOnlineModel() const
    {
        return  m_friendListOnlineModel;
    }


    QString fullName() const { return m_fullName; }
    QString avatarUser() const { return m_avatarUser; }

signals:
    void isHasUpdateMoreArticleChanged();
    void portraitModeChanged();
    void ratioChanged();
    void applicationWidthChanged();
    void dpiChanged();
    void activeUidChanged();
    void startOpeningFriendListPage();
    void logoutAccountSuccess();
    void loginSuccess();
    void positionViewAtIndexMessage();
    void isLoginChanged();
    void friendListModelChanged();
    void friendSearchModelChanged();
    void friendListSortByNameModelChanged();
    void friendListOnlineModelChanged();
    void requireLogin();
    void notifyActiveCode(int success);
    void fullNameChanged();
    void avatarUserChanged();


protected slots:
   void onHasDpiScreen(QString dpi);
   void onRequireLogin();
   void onNotifyError(int type);
   void onActiveCore(int successed);


public slots:
    void initData();
    void notifyPortraitMode();
    void onDologin(QString username, QString password);
    void onDoLogout();
    void onDoSearchFriends(QString txtInput);
    void onUpdateContactData(qint64 buddyId, qint64 uid, int totalMember);
    void onSendMessage(QString message);
    void onGetActiveCodeRequest(QString phone);
    void onMakeAcountRequest(QString phone, QString activeCode);
    void onAddFriendToGroup(qint64 uid, qint64 fid, QString displayName);
    void onDeleteFriendFromGroup(qint64 uid, qint64 fid, QString displayName);
    void onCreateGroup(QString groupName);

    ConversationModel* getConversationModel(qint64 friendId)
    {
        QMap<qint64, ConversationModel*>::iterator itr = m_mapConversationModel.find(friendId);
        if(itr != m_mapConversationModel.end())
        {
            return itr.value();
        }
        return NULL;
    }

    void insertConversationModel(qint64 buddyId, ConversationModel *conversationModel)
    {
        m_mapConversationModel.insert(buddyId, conversationModel);
    }

private slots:
    void onGetUnitFriendsProfile(QList<UserProfile*> aUnitFriends);
    void onGetAllFriendsOnline(QList<UserProfile*> listFriendsOnline);
    void onGetAllProfileFinish();
    void onGetListFriendsSortByName();
    void onGetListFriendsSortByNameFirstTime();
    void onRefreshCurrentListFriend();
    void onRequestBuddiesFinished();
    void onMessageSend(Converse::AbstractMessage *message);
    void onMessageSent(Converse::AbstractMessage *message);
    void onMessageReceived(Converse::AbstractMessage *message);
    void onMessageReadFromLocal(Converse::AbstractMessage *message);
    void onLogined(bool successed, qint64 uid);
    void onLogouted();
    void onCreateNewGroupSucessfull();

private:
    QQmlPropertyMap *m_colors;
    QQmlPropertyMap *m_constants;

    int m_applicationWidth;
    bool m_isPortraitMode;
    bool m_isMobile;
    qreal m_ratio;
    QString m_dpi;
    qint64 m_activeUid;
    bool m_isLogin;

    QString m_fullName;
    QString m_avatarUser;

    ChatManager *chatManager;

    FriendListModel* m_friendListModel;
    FriendListModel* m_friendSearchModel;
    FriendListModel* m_friendListSortByNameModel;
    FriendListModel* m_friendListOnlineModel;

    QMap<qint64, ConversationModel*> m_mapConversationModel;
};

#endif // APPLICATIONINFO_H
